﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Routing;
using System.Web.Compilation;
using System.Web.UI;

namespace mal.Web.Routing
{
    /// <summary>
    /// Handler that return the endpoint for the request
    /// </summary>
    public class PageRouteHandler : IRouteHandler
    {
        private static string routeDataValuesKey = "routeDataValuesKey";

        public static RouteValueDictionary QueryString
        {
            get
            {
                if (HttpContext.Current.Items.Contains(routeDataValuesKey))
                    return HttpContext.Current.Items[routeDataValuesKey] as RouteValueDictionary;
                else
                    return new RouteValueDictionary();
            }
            set
            {
                HttpContext.Current.Items[routeDataValuesKey] = value;
            }
        }

        public PageRouteHandler(String virtualPath)
        {
            this.VirtualPath = virtualPath;
        }

        public string VirtualPath { get; private set; }       
        

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            QueryString = requestContext.RouteData.Values;
            IHttpHandler handler = BuildManager.CreateInstanceFromVirtualPath
                    (this.VirtualPath, typeof(Page)) as IHttpHandler;
            return handler;
        }
    }
}
